package  
{
	import DeviceData.LocalDB;
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import flash.desktop.NativeApplication;
	import flash.events.FullScreenEvent;
	import Screens.CaptureScreen;
	import Screens.GalleryScreen;
	import Screens.InfoScreen;
	import Screens.LogInScreen;
	import Screens.NavigationEvent;
	import starling.display.Sprite;
	import starling.events.Event;
	import MainMenu;
	/**
	 * @author Simon Claeye
	 */
	public class Controller extends Sprite
	{
		private var navigator:ScreenNavigator;
		
		//String classes
		private static const MAIN_MENU:String = "mainMenu";
		private static const CAPTURE_SCREEN:String = "captureScreen";
		private static const INFO_SCREEN:String = "infoScreen";
		private static const GALLERY_SCREEN:String = "galleryScreen";
		private static const LOGIN_SCREEN:String = "logInScreen";
		
		//Screen items
		private var capture:CaptureScreen;

		//current screen
		private var currentScreen:String;
		
		//util
		private var database:LocalDB;
		
		public function Controller()
		{
			this.addEventListener( Event.ADDED_TO_STAGE, addedToStageHandler );
		}
		
		private function addedToStageHandler(event:Event):void
		{			
			//Setup global classes
			this.database = new LocalDB();
			
			// Instantiate Screen navigator items
			var mainMenu:ScreenNavigatorItem = new ScreenNavigatorItem(new MainMenu());
			capture = new CaptureScreen();
			var captureScreen:ScreenNavigatorItem = new ScreenNavigatorItem(capture);
			var infoscreen:ScreenNavigatorItem = new ScreenNavigatorItem( new InfoScreen(capture, database));
			var galleryScreen:ScreenNavigatorItem = new ScreenNavigatorItem(new GalleryScreen(database));
			var logInScreen:ScreenNavigatorItem = new ScreenNavigatorItem(new LogInScreen(database));
			
			this.navigator = new ScreenNavigator();
			
			this.navigator.addScreen(MAIN_MENU, mainMenu);
			this.navigator.addScreen(CAPTURE_SCREEN, captureScreen);
			this.navigator.addScreen(INFO_SCREEN, infoscreen);
			this.navigator.addScreen(GALLERY_SCREEN, galleryScreen);
			this.navigator.addScreen(LOGIN_SCREEN, logInScreen);
			
			this.addChild(navigator);
			this.navigator.showScreen(MAIN_MENU);
			this.navigator.activeScreen.addEventListener(NavigationEvent.RETURN, onReturn);
			this.navigator.activeScreen.addEventListener(NavigationEvent.MAIN_MENU, returnToMain);
			this.navigator.activeScreen.addEventListener(NavigationEvent.TAKE_IMAGES, onTakeImages);
			this.navigator.activeScreen.addEventListener(NavigationEvent.UPLOAD, onUpload);
			this.navigator.activeScreen.addEventListener(NavigationEvent.CLOSE, onClose);
			currentScreen = MAIN_MENU;
		}
		
		/**
		 * Callback function for when a screen wishes to return to the previous
		 * screen. In this case it will always be the main menu.
		 * @param	event
		 */
		private function onReturn(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(MAIN_MENU);
		}
		
		/**
		 * Callback function to navigate the application to the information
		 * screen.
		 * @param	event
		 */
		private function onEdit(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(INFO_SCREEN);
			this.navigator.activeScreen.addEventListener(NavigationEvent.RETURN, onReturn);
			this.navigator.activeScreen.addEventListener(NavigationEvent.MAIN_MENU, returnToMain);
			
			currentScreen = INFO_SCREEN;
		}
		
		/**
		 * Callback function to load the gallery screen.
		 * @param	event
		 */
		private function onUpload(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(GALLERY_SCREEN);
			this.navigator.activeScreen.addEventListener(NavigationEvent.RETURN, onReturn);
			this.navigator.activeScreen.addEventListener(NavigationEvent.MAIN_MENU, returnToMain);
			this.navigator.activeScreen.addEventListener(NavigationEvent.EDIT, onEdit);
			this.navigator.activeScreen.addEventListener(NavigationEvent.LOGING_REQUIRED, onLoginRequired);
		}
		
		/**
		 * Callback function, invoked when the user needs to log in to the website.
		 * @param	event
		 */
		private function onLoginRequired(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(LOGIN_SCREEN);
			this.navigator.activeScreen.addEventListener(NavigationEvent.RETURN, onReturn);
			this.navigator.activeScreen.addEventListener(NavigationEvent.CONTINUE, onContinue);
		}
		
		/**
		 * Callback function to load the gallery screen after succesful authentication.
		 * @param	event
		 */
		private function onContinue(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(GALLERY_SCREEN);
		}
		
		/**
		 * Callback function to return to the main menu.
		 * @param	event
		 */
		private function returnToMain(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(MAIN_MENU);
			
			currentScreen = MAIN_MENU;
		}
		
		/**
		 * Callback function to load the capture screen.
		 * @param	event
		 */
		private function onTakeImages(event:NavigationEvent):void
		{
			this.navigator.clearScreen();
			this.navigator.showScreen(CAPTURE_SCREEN);
			this.navigator.activeScreen.addEventListener(NavigationEvent.RETURN, onReturn);
			this.navigator.activeScreen.addEventListener(NavigationEvent.MAIN_MENU, returnToMain);
			this.navigator.activeScreen.addEventListener(NavigationEvent.EDIT, onEdit);
			
			currentScreen = CAPTURE_SCREEN;
		}
		
		/**
		 * Callback function to close the application on Android. This
		 * function does not work on iOS.
		 * @param	event
		 */
		private function onClose(event:NavigationEvent):void
		{
			NativeApplication.nativeApplication.exit();
		}
	}

}